
package com.jf.cloud.service.sysmanager.remote;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jf.cloud.service.sysmanager.common.db.Query;
import com.jf.cloud.service.sysmanager.common.util.DateUtil;
import com.jf.cloud.service.sysmanager.common.util.StringUtil;
import com.jf.cloud.service.sysmanager.entity.SysStaff;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jf.cloud.service.sysmanager.dao.IPubgExchangeGoodsService;
import com.jf.cloud.service.sysmanager.common.base.AjaxPageData;
import com.jf.cloud.service.sysmanager.common.base.OPaging;
import com.jf.cloud.service.sysmanager.entity.PubgExchangeGoods;
import com.jf.cloud.service.sysmanager.support.web.BaseController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工控制器
 * 
 * @author 代码机1.0 创建时间：2018-03-31
 */
@Controller
@RequestMapping("/pubgExchangeGoods")
public class PubgExchangeGoodsController extends BaseController {

	/**
	 * 日志
	 */
	private static Log log = LogFactory.getLog(PubgExchangeGoodsController.class);

	/**
	 * 员工服务
	 */
	@Resource
	private IPubgExchangeGoodsService pubgExchangeGoodsService;

	/**
	 * 后台商城物品管理页面
	 * 
	 * @author 高冬冬 2018年4月2日
	 */
	@RequestMapping("/list")
	public String list(HttpServletRequest request) {
		return "jsp/pubg/pubgExchangeGoods_list";
	}

	/**
	 * 后台获取商城物品管理列表
	 *
	 * @author 高冬冬 2018年4月2日
	 */
	@ResponseBody
	@RequestMapping("/listPubgExchangeGoods.json")
	public AjaxPageData<PubgExchangeGoods> listPubgExchangeGoods(HttpServletRequest request, OPaging<PubgExchangeGoods> page) {
		return getAjaxPageData(pubgExchangeGoodsService.findPage(page));
	}

	/**
	 * 商城物品编辑
	 *
	 * @author 高冬冬 2018年4月2日
	 */
	@RequestMapping("/pubgExchangeGoodsEditer")
	public String pubgExchangeGoodsEditer(HttpServletRequest request,HttpServletResponse response) {
		Long id = getLongValue(request, "rowId");
		PubgExchangeGoods pubgExchangeGoods = new PubgExchangeGoods();
		if (id != null && id > 0) {
			pubgExchangeGoods = pubgExchangeGoodsService.findByPK(id);
		}
		request.setAttribute("pubgExchangeGoods", pubgExchangeGoods);
		return "jsp/pubg/pubgExchangeGoods_editer";
	}

	/**
	 * 验证商城物品名是否存在
	 *
	 * @author 高冬冬 2018年4月2日
	 */
	@ResponseBody
	@RequestMapping("/checkGoodsName")
	public Map<String, Object> checkGoodsName(HttpServletRequest request, Long rowId, String goodsName) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", "-1");
		try {
			if (StringUtil.isNotEmpty(goodsName)) {
				if (goodsName.length() > 20) {
					result.put("code", "-2");
					result.put("message", "商城物品名称最多输入20个字符！");
					return result;
				}
				Query query = new Query();
				query.put("_NOT_EQUAL$ROW_ID", rowId);
				query.put("GOODS_NAME", goodsName);
				List<PubgExchangeGoods> goodsList = pubgExchangeGoodsService.findList(query);
				if (goodsList != null && goodsList.size() > 0) {
					result.put("code", "-2");
					result.put("message", "商城物品名称已存在！");
					return result;
				}
			}
			result.put("code", "0");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 添加/编辑商城物品
	 *
	 * @author 高冬冬 2018年4月2日
	 */
	@RequestMapping("/savePubgExchangeGoods")
	public String savePubgExchangeGoods(HttpServletRequest request, PubgExchangeGoods pubgExchangeGoods) {
		String returnView = list(request);
		if (isReCommit(request)) {
			return returnView;
		}
		request.setAttribute("code", 1);
		SysStaff user = getLoginUser(request);
		try {
			if (StringUtil.isEmpty(pubgExchangeGoods.getGoodsName())) {
				request.setAttribute("msg", "商城物品名称不能为空!");
				return returnView;
			}
			if (pubgExchangeGoods.getGoodsName().length() > 20) {
				request.setAttribute("msg", "商城物品名称最多输入20个字符!");
				return returnView;
			}
			if (StringUtil.isEmpty(pubgExchangeGoods.getGoodsPrice())) {
				request.setAttribute("msg", "物品价格不能为空!");
				return returnView;
			}
			if (pubgExchangeGoods.getGoodsPrice() <= 0 || pubgExchangeGoods.getGoodsPrice() > 1000000) {
				request.setAttribute("msg", "物品价格0到100万之间!");
				return returnView;
			}
			if (StringUtil.isEmpty(pubgExchangeGoods.getOpenFlag())) {
				request.setAttribute("msg", "状态不能为空!");
				return returnView;
			}
			pubgExchangeGoods.setGoodsTypeId(1l);
			pubgExchangeGoods.setGoodsTypeName("普通物品");
			pubgExchangeGoods.setOptId(user.getRowId());
			pubgExchangeGoods.setOptAccount(user.getStaffAccount());
			pubgExchangeGoods.setOptTime(DateUtil.getCurrentDateTime());
			if (pubgExchangeGoods.getRowId() == null || pubgExchangeGoods.getRowId() == 0) {
				this.pubgExchangeGoodsService.saveOrUpdatePubgExchangeGoods(pubgExchangeGoods);
				request.setAttribute("msg", "添加商城物品成功!");
				log.info(user.getStaffAccount() + " 添加商城物品");
			} else {
				this.pubgExchangeGoodsService.updatePubgExchangeGoods(pubgExchangeGoods);
				request.setAttribute("msg", "修改商城物品成功!");
				log.info(user.getStaffAccount() + " 修改商城物品");

			}
		} catch (Exception e) {
			log.error("savePubgExchangeGoods ---------> has error",e);
			request.setAttribute("msg", "保存商城物品失败,未知错误!");
			return returnView;
		}
		return returnView;
	}

	/**
	 * 修改商城物品启用状态
	 *
	 * @author 高冬冬 2018年4月2日
	 */
	@ResponseBody
	@RequestMapping("/updateOpenFlag")
	public String updateOpenFlag(HttpServletRequest request, Long rowId, Integer openFlag) {
		String result = "-2";
		try {
			if (rowId == null || openFlag == null) {
				return "2";
			}
			SysStaff user = getLoginUser(request);
			// 默认更新失败
			result = "2";
			List<Object> params = new ArrayList<Object>();
			params.add(openFlag);
			params.add(user.getRowId());
			params.add(user.getStaffAccount());
			params.add(DateUtil.getCurrentDateTime());
			params.add(rowId);
			pubgExchangeGoodsService.update("UPDATE pubg_exchange_goods SET OPEN_FLAG=?, OPT_ID=?, OPT_ACCOUNT=?, OPT_TIME=? WHERE ROW_ID=? ", params.toArray());
			result = "1";
		} catch (Exception e) {
			log.error("修改商城物品启用状态出错了", e);
		}
		return result;
	}

}
